package com.platform.api.bzproject.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.platform.api.bzproject.dao.BzAllWallpaperMapper;
import com.platform.api.bzproject.model.BzAllWallpaper;
import com.platform.api.bzproject.model.BzTalent;
import com.platform.api.film.model.FilmShort;
import com.platform.api.film.query.MovieVo;
import com.platform.api.film.service.FilmShortService;
import com.platform.common.exception.PlatformBindException;
import com.platform.security.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author guoyunfei on 2021/9/25 15:30
 * @className DouyinWallpaperService
 * @description TODO
 * @Version 1.0
 */

@Service
@Transactional(rollbackFor=Exception.class)
public class BzAllWallpaperService extends ServiceImpl<BzAllWallpaperMapper, BzAllWallpaper> {


    @Autowired
    private BzAllWallpaperMapper bzAllWallpaperDao;


    @Autowired
    private FilmShortService filmShortService;

    @Autowired
    private IBzTalentService iBzTalentService;



    private final String ORDER_BY_ID = "id desc";
    private final String ORDER_BY_BROEWSE_COUNT = "browse_count desc";
    private final String ORDER_BY_LIKE_COUNT = "like_count desc";
    private final String ORDER_BY_COLLECTION_COUNT = "collection_count desc";
    private final String ORDER_BY_DOWN_COUNT = "down_count desc";
    //置顶并且 按照时间
    private final String ORDER_BY_TOP_AND_TIME = "topping desc,wp.id desc";

    /**
     * 设置排序
     * @param order
     * @return
     */
    public String getOrderBy(Integer order){
        if(null == order){
            return ORDER_BY_ID;
        }
        if(0 == order){
            return ORDER_BY_BROEWSE_COUNT;
        }else if(1 == order){
            return ORDER_BY_LIKE_COUNT;
        }else if(2 == order){
            return ORDER_BY_COLLECTION_COUNT;
        }else if(3 == order){
            return ORDER_BY_DOWN_COUNT;
        }else if(4 == order){
            return ORDER_BY_TOP_AND_TIME;
        }
        return ORDER_BY_ID;
    }


    public PageInfo<BzAllWallpaper> selectAllWallpaper(BzAllWallpaper wallpaper, int pageNum, int pageSize, String order){
        PageHelper.startPage(pageNum, pageSize).setOrderBy(order);
        wallpaper.setIsDelete(1);
        final PageInfo<BzAllWallpaper> userPageInfo = new PageInfo<>(bzAllWallpaperDao.selectAllWallpaper(wallpaper));
        return userPageInfo;
    }

    public BzAllWallpaper getWallpaperById(Long id){
        BzAllWallpaper bzAllWallpaper = bzAllWallpaperDao.selectOne(new LambdaQueryWrapper<BzAllWallpaper>()
                .eq(BzAllWallpaper::getId, id).select(BzAllWallpaper::getId, BzAllWallpaper::getOpenid, BzAllWallpaper::getImage,
                        BzAllWallpaper::getThumbnail, BzAllWallpaper::getTitle, BzAllWallpaper::getNumber,BzAllWallpaper::getFilmId));

        return bzAllWallpaper;
    }

    /**
     * 获取用户当天已上传的壁纸数量
     * @param openid
     * @return
     */
    public int getUploadCount(String openid){
        return bzAllWallpaperDao.getUploadCount(openid);
    }

    public BzAllWallpaper getLimitOneRight(Map<String,Object> map){
        return bzAllWallpaperDao.getLimitOneRight(map);
    }
    public BzAllWallpaper getLimitOneLeft(Map<String,Object> map){
        return bzAllWallpaperDao.getLimitOneLeft(map);
    }

    public BzAllWallpaper upAndDownData(Map<String,Object> map){
        if(map.get("flag").toString().equals("left")){
            return bzAllWallpaperDao.getLimitOneLeft(map);
        }else if(map.get("flag").toString().equals("right")){
            return bzAllWallpaperDao.getLimitOneRight(map);
        }
        return null;
    }

    public int updateDownCount(Long wId){
        return bzAllWallpaperDao.updateDownCount(wId);
    }

    public List<BzAllWallpaper> getAllUserData(){
        return bzAllWallpaperDao.getAllUserData();
    }

    @Transactional(rollbackFor = Exception.class)
    public int updateAllUserData(List<BzAllWallpaper> list){
        return bzAllWallpaperDao.updateAllUserData(list);
    }

    /**
     * 修改用户所有数据
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int updateAllUserData(){
        List<BzAllWallpaper> allUserData = bzAllWallpaperDao.getAllUserData();
        int i = updateAllUserData(allUserData);
        return i;
    }

    public List<Integer> getUserClassIds(String userId){
        return bzAllWallpaperDao.getUserClassIds(userId);
    }

    public BzAllWallpaper getDetail(Long id){
        MovieVo movieVo = new MovieVo();
        movieVo.setResourceId(id);
        return get(movieVo);
    }

    public BzAllWallpaper get(MovieVo movieVo){
        BzAllWallpaper wallpaper = bzAllWallpaperDao.selectOne(new LambdaQueryWrapper<BzAllWallpaper>()
                .select(BzAllWallpaper::getId, BzAllWallpaper::getTitle, BzAllWallpaper::getFilmCategoryId,
                        BzAllWallpaper::getFilmId, BzAllWallpaper::getNumber, BzAllWallpaper::getImage,
                        BzAllWallpaper::getThumbnail, BzAllWallpaper::getSort, BzAllWallpaper::getName,
                        BzAllWallpaper::getTopping)
                .eq(Objects.nonNull(movieVo.getResourceId()), BzAllWallpaper::getId, movieVo.getResourceId())
                .eq(Objects.nonNull(movieVo.getNumber()), BzAllWallpaper::getNumber, movieVo.getNumber())
                .eq(Objects.nonNull(movieVo.getFilmId()), BzAllWallpaper::getFilmId, movieVo.getFilmId())
                .eq(BzAllWallpaper::getIsDelete, 1).last("limit 1"));
        if(Objects.isNull(wallpaper)){
            throw new PlatformBindException("无数据记录");
        }
        return wallpaper;
    }
    public boolean saveResource(BzAllWallpaper bzAllWallpaper){

        String userId = SecurityUtils.getUser().getUserId();
        bzAllWallpaper.setOpenid(userId);

        BzTalent bzTalent = iBzTalentService.getOne(
                new LambdaQueryWrapper<BzTalent>().eq(BzTalent::getUserid, userId)
                        .eq(BzTalent::getStatus,2)
                        .last("limit 1"));
        if(Objects.isNull(bzTalent)){
            throw new PlatformBindException("您不是创作者");
        }

        //添加压缩参数
        //String compress = OssProcess.getCompress(attachFileService.getOssType());

        //String thumbnail = bzAllWallpaper.getThumbnail();
        //压缩封面图
        //bzAllWallpaper.setThumbnail(thumbnail + compress);
        //设置低画质图片
        //bzAllWallpaper.setLowImageQuality(thumbnail + OssProcess.getCompressNoAd(attachFileService.getOssType()));

        bzAllWallpaper.setCreateTime(new Date());
        bzAllWallpaper.setTime(new Date());

        //设置影视相关信息
        FilmShort filmShort = filmShortService.getById(bzAllWallpaper.getFilmId());
        if(Objects.nonNull(filmShort)){
            bzAllWallpaper.setFilmCategoryId(filmShort.getFilmCategoryId());
            bzAllWallpaper.setFilmId(filmShort.getId());
        }
        //先默认使用2
        bzAllWallpaper.setClassId(2);
        return saveOrUpdate(bzAllWallpaper);
    }
}
